
//
//  ShareView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/5.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class ShareView: UIView, BottomShadow, AddCornerUIRectCorner {
    
    private let mainVew = UIView()
    private let lrMargin: CGFloat = 26
    private let buttonLRMargin: CGFloat = 20
    private let shareTitleTopMargin: CGFloat = 20
    private let buttonWidth: CGFloat = 43
    private let buttonHeight: CGFloat = 66
    private var didSelectIndexClosure:((NSInteger) -> Void)?
    open func showShareView() {
        
        let window = UIApplication.shared.keyWindow
        self.frame = window!.bounds
        self.alpha = 0.3
        self.backgroundColor = ONColor.maskBGColor
        window?.addSubview(self)
        UIView.animate(withDuration: TimeInterval(anTIME)) {
            self.alpha = 1
        }
        
        mainVew.frame = CGRect(x: lrMargin, y: 0, width: ScreenWidth - lrMargin * 2, height: 0)
        mainVew.backgroundColor = UIColor.white
        self.addSubview(mainVew)
        
        let shareTitle = UILabel()
        shareTitle.text = "分享到:"
        shareTitle.textColor = ONColor.subTextColor
        shareTitle.font = UIFont.systemFont(ofSize: 12)
        shareTitle.frame = CGRect(x: buttonLRMargin, y: shareTitleTopMargin, width: 0, height: 0)
        shareTitle.sizeToFit()
        mainVew.addSubview(shareTitle)
        
        let datas = [("qq_icon", "QQ"), ("weibo_icon", "微博"), ("circle_of_friends", "朋友圈"),("weChat_icon", "微信"), ("btn_more_share", "更多")]
        var bottomY: CGFloat = 0
        for (index, item) in datas.enumerated() {
            let button = ShareButton(title: item.1, imageName: item.0)
            button.tag = index
            button.addTarget(self, action: #selector(shareTap(_ :)), for: .touchUpInside)
            let count = CGFloat(datas.count - 1)
            let margin = (mainVew.width - buttonLRMargin * 2 - CGFloat(datas.count) * buttonWidth) / count
            button.frame = CGRect(x: CGFloat(index) * buttonWidth + buttonLRMargin + CGFloat(index) * margin, y: shareTitle.bottom + 10, width: buttonWidth, height: buttonHeight)
            mainVew.addSubview(button)
            bottomY = button.bottom
        }
        
        let bottomButton = UIButton(frame: CGRect(x: 0, y: bottomY + 20, width: mainVew.width, height: 37))
        bottomButton.backgroundColor = UIColor(hex: 0x01A1EB)
        bottomButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        bottomButton.setTitle("取消", for: .normal)
        bottomButton.addTarget(self, action: #selector(cancelTap), for: .touchUpInside)
        mainVew.addSubview(bottomButton)
        
        mainVew.height = bottomButton.bottom
        mainVew.center = CGPoint(x: self.width / 2, y: self.height / 2)
        setShadow(view: mainVew, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        addCornerWithView(aView: bottomButton, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
    }
    
    @objc private func cancelTap() {
        hide()
    }
    
    private func hide() {
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.alpha = 0.3
        }) { (make) in
            self.removeFromSuperview()
        }
    }
    
    @objc private func shareTap(_ btn: UIButton) {
        hide()
    }
    
}

fileprivate class ShareButton: UIButton {
    private let buttonTitleHeight: CGFloat = 15
    private let titleWithImageMargin: CGFloat = 10
    private var shareTitle = ""
    private var shareImageName = ""
    init(title: String, imageName: String) {
        super.init(frame: .zero)
        shareTitle = title
        shareImageName = imageName
        setUI()
    }
    
    func setUI() {
        self.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        self.titleLabel?.textAlignment = .center
        self.setTitleColor(ONColor.subTextColor, for: .normal)
        self.setImage(UIImage(named: shareImageName), for: .normal)
        self.setTitle(shareTitle, for: .normal)
        self.adjustsImageWhenHighlighted = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        let rect = CGRect(x: 0, y: contentRect.size.height - buttonTitleHeight, width: contentRect.width, height: buttonTitleHeight)
        return rect
    }
    
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        let rect = CGRect(x: 0, y: 0, width: contentRect.size.width, height: contentRect.height - titleWithImageMargin - buttonTitleHeight)
        return rect
    }
}
